﻿using System;
using System.Web;
using System.Web.UI;
using STT.Business;
using STT.Entities;

namespace STT.Admin.UserControls
{
    public partial class ucThemKhoaHoc : System.Web.UI.UserControl
    {
        int MaKH = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmpDate.ValueToCompare = DateTime.Now.ToString("yyyy-MM-dd");

            btThemMoi.Visible = true;
            btLuu.Visible = false;
            btHuy.Visible = true;

            if (Request.QueryString["idkh"] != null)
            {
                MaKH = int.Parse(Request.QueryString["idkh"]);
                LoadKhoaHocSua();
                btLuu.Visible = true;

                btThemMoi.Visible = false;
                btHuy.Visible = false;

                cmpDate.ValueToCompare = txtThoiGianBatDau.Text;
            }
        }

        bool KiemTraDuLieu()
        {
            return BTbKhoaHoc.KiemTraDuLieu(new ETbKhoaHoc
            {
                TenKhoaHoc = txtTenKhoaHoc.Text,
                ThoiGianBatDau = DateTime.Parse(txtThoiGianBatDau.Text)
            });
        }

        private void LoadKhoaHocSua()
        {
            ETbKhoaHoc khoahoc = BTbKhoaHoc.SelectByID(MaKH);

            txtTenKhoaHoc.Text = khoahoc.TenKhoaHoc;
            txtDiaDiem.Text = khoahoc.DiaDiemHoc;
            txtThoiGianBatDau.Text = khoahoc.ThoiGianBatDau.ToString("yyyy-MM-dd");
            txtThoiGianKetThuc.Text = khoahoc.ThoiGianKetThuc.ToString("yyyy-MM-dd");
            txtNoiDung.Text =HttpUtility.HtmlDecode(khoahoc.NoiDung);
        }

        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            if (txtNoiDung.Text == "")
            {
                ThongBao("Bạn chưa nhập nội dung khóa học.");
                return;
            }

            if (!KiemTraDuLieu())
            {
                BTbKhoaHoc.Insert(new ETbKhoaHoc
                {
                    TenKhoaHoc = txtTenKhoaHoc.Text,
                    NoiDung = txtNoiDung.Text,
                    DiaDiemHoc = txtDiaDiem.Text,
                    ThoiGianBatDau = DateTime.Parse(txtThoiGianBatDau.Text),
                    ThoiGianKetThuc = DateTime.Parse(txtThoiGianKetThuc.Text),
                    FileDinhKem = "",
                    NguoiTao=Session["TenDN"].ToString()
                });

                //txtNoiDung.Text = HttpUtility.HtmlDecode(txtNoiDung.Text);
                Response.Redirect("~/Admin/Default.aspx?idpage=tkh", true);
                Session["action"] = 1;
            }
            else
            {
                ThongBao("Khóa học này đã tồn tại!");
            }
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void btLuu_Click(object sender, EventArgs e)
        {
            BTbKhoaHoc.Update(new ETbKhoaHoc
            {
                MaKhoaHoc = MaKH,
                TenKhoaHoc = txtTenKhoaHoc.Text,
                NoiDung = txtNoiDung.Text,
                DiaDiemHoc = txtDiaDiem.Text,
                ThoiGianBatDau = DateTime.Parse(txtThoiGianBatDau.Text),
                ThoiGianKetThuc = DateTime.Parse(txtThoiGianKetThuc.Text),
                FileDinhKem=""
            });

            //txtNoiDung.Text = HttpUtility.HtmlDecode(txtNoiDung.Text);
            Session["action"] = 2;
            Response.Redirect("~/Admin/Default.aspx?idpage=tkh", true);

        }

        protected void btHuy_Click(object sender, EventArgs e)
        {
            txtTenKhoaHoc.Text = "";
            txtDiaDiem.Text = "";
            txtThoiGianBatDau.Text = "";
            txtThoiGianKetThuc.Text = "";
            txtNoiDung.Text = "";
        }
    }
}